package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Damage;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDamageService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.LogicException;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("damage")
public class DamageController {

    @Autowired
    IDamageService damageService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"damage:view","赔损明细页面"},logical = Logical.OR)
    public String view() {
        return "damage";
    }

    @RequestMapping("query")
    @ResponseBody
    @RequiresPermissions(value = {"damage:query","赔损明细列表"},logical = Logical.OR)
    public PageResult query(QueryObject qo) {
        return damageService.query(qo);
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<Damage> selectAll() {
        return damageService.selectAll();
    }

    @RequestMapping("update")
    @ResponseBody
    @RequiresPermissions(value = {"damage:update","赔损明细更改"},logical = Logical.OR)
    public JsonResult saveOrUpdate(Damage damage) {
        try {
                damageService.updateByPrimaryKey(damage);
            return JsonResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.mark("保存失败");
        }
    }

    @RequestMapping("changeState")
    @ResponseBody
    @RequiresPermissions(value = {"department:changeState","赔损明细拨款"},logical = Logical.OR)
    public JsonResult changeState(Long id) {
        try {
            damageService.changeState(id);
            return JsonResult.success("");
        }catch (LogicException e) {
            e.printStackTrace();
            return JsonResult.mark(e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return JsonResult.mark("操作失败");
        }
    }
}
